package com.bgi.flutterbluetooth;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import io.flutter.Log;
public class BluetoothMsg {

    //推送的内容码
    //蓝牙状态
    private static final int PUSHCODE_BLUETOOTH_STATE = 21;
    //发现蓝牙
    private static final int PUSHCODE_FIND_BLUETOOTH = 22;
    //蓝牙扫描状态
    private static final int PUSHCODE_SCAN_STATE = 23;
    //读取到的蓝牙信息
    private static final int PUSHCODE_READ_MSG = 24;
    //蓝牙连接状态
    private static final int PUSHCODE_BLUETOOTH_CONNECTED_STATE = 25;

    //蓝牙状态码 -1 设备不支持蓝牙 10 关闭 11 正在打开 12 打开 13 正在关闭
    public static final int STATE_NO_SUPPORT = -1;
    public static final int STATE_OFF = 10;
    public static final int STATE_TURNING_ON = 11;
    public static final int STATE_ON = 12;
    public static final int STATE_TURNING_OFF = 13;
    //蓝牙扫描状态码
    public static final int SCAN_STATE_START = 11;
    public static final int SCAN_STATE_OVER = 12;
    //蓝牙连接状态码
    public static final int CONNECT_STATE_CONNECTED = 13;
    public static final int CONNECT_STATE_UNCONNECT = 14;
    public static final int CONNECT_STATE_FAIL = 15;

    //推送的数据
    //推送的消息码
    private static final String PUSHCODE = "pushCode";
    //蓝牙状态
    private static final String STATE = "state";
    //蓝牙扫描状态
    private static final String SCANSTATE = "scanState";
    //蓝牙连接状态
    private static final String CONNECTEDSTATE = "connectedState";
    //蓝牙名称
    private static final String NAME = "name";
    //蓝牙地址
    private static final String ADDRESS = "address";
    //读取到的信息
    private static final String MSG = "msg";
    private static final String TAG = "BluetoothMsg";
    public static String createDeviceJson(String name, String address){
        JSONObject jo = new JSONObject();
        try {
            jo.put(PUSHCODE, PUSHCODE_FIND_BLUETOOTH);
            jo.put(NAME, name);
            jo.put(ADDRESS, address);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jo.toString();
    }

    public static String createStateJson(int state){
        JSONObject jo = new JSONObject();
        try {
            jo.put(PUSHCODE, PUSHCODE_BLUETOOTH_STATE);
            jo.put(STATE, state);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jo.toString();
    }

    public static String createScanStateJson(int scanState){
        JSONObject jo = new JSONObject();
        try {
            jo.put(PUSHCODE, PUSHCODE_SCAN_STATE);
            jo.put(SCANSTATE, scanState);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jo.toString();
    }

    public static String createReadMsgJsonSWI(byte[] msg){
        String[] str=bytesToHexStrings(msg);
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < str.length; i++) {
            sb.append(str[i]);
        }
        JSONObject jo = new JSONObject();
        try {
            jo.put(PUSHCODE, PUSHCODE_READ_MSG);
            jo.put(MSG, sb.toString().toUpperCase());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jo.toString();
    }

    public static String createReadMsgJson(String msg){
        JSONObject jo = new JSONObject();
        try {
            jo.put(PUSHCODE, PUSHCODE_READ_MSG);
            jo.put(MSG, msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jo.toString();
    }

    public static String[] bytesToHexStrings(byte[] src){

        if (src == null || src.length <= 0) {
            return null;
        }

        String[] str = new String[src.length];
        for (int i = 0; i < src.length; i++) {

            int v = src[i] & 0xFF;

            String hv = Integer.toHexString(v);

            if (hv.length() < 2) {

                str[i] = "0"+hv;
            }else{
                str[i] = hv;
            }

        }
        return str;
    }

    public static String createConnectedStateJson(int connectedState, String name, String address){
        JSONObject jo = new JSONObject();
        try {
            jo.put(PUSHCODE, PUSHCODE_BLUETOOTH_CONNECTED_STATE);
            jo.put(CONNECTEDSTATE, connectedState);
            jo.put(NAME, name);
            jo.put(ADDRESS, address);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jo.toString();
    }

}
